home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SearchProcs and Color Sep.c
-
- Contains: This snippet shows how to create and install your
- own custom search procedure. In this example, the
- searchProc performs RGB color separation by checking
- to see if the individual RGB component values fall
- within a certain maximum and minimum threshold level.
-
- Written by: Edgar Lee
-
- Copyright: Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/14/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
- /****************************************************************************/
-
- #include <Types.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Events.h>
- #include <Windows.h>
- #include <Quickdraw.h>
- #include <QDOffscreen.h>
- #include <Fonts.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
-
-
- /* Constant Declarations */
-
- #define WWIDTH 500
- #define WHEIGHT 185
-
- #define WLEFT (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
- #define WTOP (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
-
- #define kHiliteCutoff 60000 /* threshold levels */
- #define kShadowCutoff 9000 /* threshold levels */
-
- enum {
- tRedSeparation = 0,
- tGreenSeparation,
- tBlueSeparation
- };
-
- /* Global Variable Definitions */
-
- WindowPtr gWindow;
- short gColorType;
-
- void initMac();
- void createWindow();
- void doEventLoop();
-
- void doColorSeparation();
- static pascal Boolean searchProc();
-
- void main()
- {
- initMac();
- createWindow();
- doEventLoop();
- }
-
- void initMac()
- {
- MaxApplZone();
-
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
- void createWindow()
- {
- Rect rect;
-
- SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
-
- gWindow = NewCWindow( 0L, &rect, "\pSearchProcs & Color Separation", true, documentProc,
- (WindowPtr)-1L, true, 0L );
-
- SetPort( gWindow );
- }
-
- void doColorSeparation()
- {
- short i;
- PicHandle pict; /* Pict used for the source. */
- GWorldPtr source; /* Gworld used to store the pict. */
- PixMapHandle sourcePixMap; /* Handle to the source pixmap. */
- Rect rect; /* Bounding rect of source and destination. */
- CGrafPtr currentPort; /* Saved CGrafPtr for later restore. */
- GDHandle currentDevice; /* Saved device for later restore. */
- ColorSearchUPP searchProcUPP=NewColorSearchProc(searchProc);
-
- /* Load the pict resource to be used for the source. */
- pict = (PicHandle)GetResource( 'PICT', 128 );
-
- rect = (**pict).picFrame;
- OffsetRect( &rect, -rect.left, -rect.top );
-
- /* Draw the source image in the window to see what it looks like. */
- DrawPicture( pict, &rect );
- HPurge( (Handle)pict );
-
- /* Create a gworld to store the pict. */
- NewGWorld( &source, 8, &rect, GetCTable( 8 + 72 ), nil, 0 );
- sourcePixMap = GetGWorldPixMap( source );
-
- /* Draw the pict into the gworld. */
- GetGWorld( ¤tPort, ¤tDevice );
- SetGWorld( source, nil );
-
- DrawPicture( pict, &rect );
- SetGWorld( currentPort, currentDevice );
-
- /* Do the color separation using the custom searchProcs. */
-
- AddSearch(searchProcUPP);
-
- for (i = 0; i < 3; i++)
- {
- OffsetRect( &rect, (**sourcePixMap).bounds.right, 0 );
- gColorType = i;
-
- /* Get a new seed for the offscreen ctable to insure that the searchProc is called. */
- (**(**sourcePixMap).pmTable).ctSeed = GetCTSeed();
-
- /* Copy the source to the window while using the custom searchProc. */
- CopyBits( (BitMap *)*sourcePixMap, &gWindow->portBits,
- &(**sourcePixMap).bounds, &rect, srcCopy, 0l );
- }
-
- DelSearch(searchProcUPP);
-
- /* Release the offscreen memory. */
- DisposeGWorld( source );
- }
-
- static pascal Boolean searchProc(RGBColor* color, long* position )
- {
- #pragma unused(position)
- RGBColor allWhite = { 0xffff, 0xffff, 0xffff };
- RGBColor allBlack = { 0, 0, 0 };
-
- if( gColorType == tRedSeparation )
- {
- /* hilite range */
- if( color->red > kHiliteCutoff )
- {
- /* all colors go to white */
- *color = allWhite;
- }
-
- /* shadow range */
- else if( color->red < kShadowCutoff )
- {
- /* all colors go to black */
- *color = allBlack;
- }
- /* middle range */
-
- else
- {
- /* just show red component */
- color->green = 0;
- color->blue = 0;
- }
- }
- else if( gColorType == tGreenSeparation )
- {
- /* hilite range */
- if( color->green > kHiliteCutoff )
- {
- /* all colors go to white */
- *color = allWhite;
- }
-
- /* shadow range */
- else if( color->green < kShadowCutoff )
- {
- /* all colors go to black */
- *color = allBlack;
- }
-
- /* middle range */
- else
- {
- /* show green component */
- color->red = 0;
- color->blue = 0;
- }
-
- }
- else if( gColorType == tBlueSeparation )
- {
- /* hilite range */
- if( color->blue > kHiliteCutoff )
- {
- /* all colors go to white */
- *color = allWhite;
- }
-
- /* shadow range */
- else if( color->blue < kShadowCutoff )
- {
- /* all colors go to black */
- *color = allBlack;
- }
-
- /* middle range */
- else
- {
- /* show greens */
- color->red = 0;
- color->green = 0;
- }
- }
-
- return false;
- }
-
- void doEventLoop()
- {
- EventRecord event;
- WindowPtr window;
- short clickArea;
- Rect screenRect;
-
- for (;;)
- {
- if (WaitNextEvent( everyEvent, &event, 0, nil ))
- {
- if (event.what == mouseDown)
- {
- clickArea = FindWindow( event.where, &window );
-
- if (clickArea == inDrag)
- {
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( window, event.where, &screenRect );
- }
- else if (clickArea == inContent)
- {
- if (window != FrontWindow())
- SelectWindow( window );
- }
- else if (clickArea == inGoAway)
- if (TrackGoAway( window, event.where ))
- return;
- }
- else if (event.what == updateEvt)
- {
- window = (WindowPtr)event.message;
- SetPort( window );
-
- BeginUpdate( window );
- doColorSeparation();
- EndUpdate( window );
- }
- }
- }
- }